####################################################################
#### Replication data for: #########################################
###  Hemesath, S. and Tepe, M. (2022) "Framing the approval to     #
###  test self-driving cars on public roads. the effect of safety  #
###  and competitiveness on citizens' agreement,"                  #
###  Technology in Society, p. 102177.                             #
###  Available at: https://doi.org/10.1016/j.techsoc.2022.102177.  #
####################################################################


###################
##load packages####
###################

library(tidyverse)
library(psych)
library(cjoint)
library(cregg)
library(dplyr)
library(ggplot2)
library(Hmisc)
library(table1)
library(ggh4x)
library(sjPlot)
library(performance)
library(ggeffects)
library(nFactors)
library(ggridges)

rm(list=ls())

df <- readRDS("Hemesath_Tepe_Framing_TiS_Replication.RDS")
names(df)

###Validity Checks

names(df)

df$durationmin <- df$duration/60

DE <- df[df$sample=="DE",]
US <- df[df$sample=="US",]
JP <- df[df$sample=="JP",]

quantile(DE$durationmin, probs = seq(0, 1, 1/20))
DE <- DE[DE$durationmin >6.16 & DE$durationmin < 52.25,]

quantile(US$durationmin, probs = seq(0, 1, 1/20))
US <- US[US$durationmin > 6.29 & US$durationmin < 41.61,]

quantile(JP$durationmin, probs = seq(0, 1, 1/20))
JP <- JP[JP$durationmin > 6.19 & JP$durationmin <60,]

table(df$sample)

df <- rbind(DE,US,JP)

########################
#### Data Preparation ##
########################

df$sample <- as.factor(df$sample)
df$frame <- as.factor(df$frame)
df$edu <- df$education
levels(df$edu) <- c("Low","Low","Low","Medium","High","High","Low","Low","Medium","Medium","High","High","High","High","Low","Low","Medium","High","High","High","High")
df$edu <- as.factor(as.character(df$edu))
df$edu <- fct_relevel(df$edu,"High",after=2)
df$gender <- ifelse(df$gndr=="Non-Binary",NA,as.character(df$gndr))
df$gender <- as.factor(df$gender)
df$agegrp <- cut_number(df$age,4)
df$admanx <- df$ADMuneasy
df$ecoLR <- as.numeric(df$ecoLR)
df$tech3 <- recode(df$tech3,5,4,3,2,1)
df$tech5 <- recode(df$tech5,5,4,3,2,1)
df$ADMmakeeasy <- recode(df$ADMmakeeasy,5,4,3,2,1)

table(df$agegrp,df$sample)

#################################################
### Build Negative Attitudes Algorithms Scale ###
#################################################

ADM <- select(df,c(ADMmakeeasy,admanx,ADMpreferhuman))
alpha(ADM)
df$admatt <- (df$ADMmakeeasy+df$admanx+df$ADMpreferhuman)/3

#######################################
### Build Technology Optimism Scale ###
#######################################

Tech <- select(df,c(tech1:tech9))
cor.plot(Tech)

## Exclude items 2 and 3 from analysis

Tech <- select(Tech,-c(tech2,tech3))
cor.plot(Tech)

KMO(Tech)
cortest.bartlett(Tech)

ev <- eigen(cor(Tech))
ap <- parallel(subject=nrow(Tech),var=ncol(Tech),
               rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

### Suggests 1 Factor
faTech <- fa(Tech,nfactors=1,rotate="oblique",fm="pa")
faTech
fa.diagram(faTech)
alpha(Tech)

#Build index
df$tech <- ((df$tech1+df$tech4+df$tech5+df$tech6+df$tech7+df$tech8+df$tech9)/7)

############################
###Regression Models #######
############################

m1 <- lm(framingDV ~ frame+sample+agegrp+gender+edu,data=df)
m2 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+
           frame*sample,data=df)
m3 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+
           frame*gender,data=df)
m4 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+
           frame*agegrp,data=df)
m5 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+admatt+
           frame*admatt,data=df)
m6 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+tech+
           frame*tech,data=df)
m7 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+ecoLR+
           frame*ecoLR,data=df)
m8 <- lm(framingDV ~ frame+sample+frame*sample+agegrp+gender+edu+ecoLR+tech+admatt+
           frame*gender+frame*agegrp+frame*ecoLR+frame*tech+frame*admatt,data=df)

##################################
### Estimate Marginal Effects  ###
##################################
me1 <- ggeffect(m1,terms=c("frame"))
me2 <- ggeffect(m2,terms=c("frame","sample"))
me3 <- ggeffect(m3,terms=c("frame","gender"))
me4 <- ggeffect(m4,terms=c("frame","agegrp"))
me5 <- ggeffect(m5, c("admatt[all]", "frame"), ci.lvl = 0.95,interactive=T)
me6 <- ggeffect(m6, c("tech[all]", "frame"), ci.lvl = 0.95,interactive=T)
me7 <- ggeffect(m7, c("ecoLR[all]", "frame"), ci.lvl = 0.95,interactive=T)

##########################
### Regression Tables  ###
##########################

tab2 <- sjPlot::tab_model(m1,m2,m3,m4,m5,m6,m7,m8,
                          vcov.type = "HC3",
                          show.se = TRUE,
                          show.ci=F,
                          p.style = "stars",
                          collapse.se = T,
                          dv.labels = c("(1)", "(2)", "(3)", "(4)","(5)","(6)","(7)","(8)"),
                          show.reflvl=T,
                          auto.label=F,
                          show.fstat=T,
                          show.aic=T,
                          show.loglik = T,
                          pred.labels=c(
                            "(Intercept)",
                            "Competitiveness with Safety Loss",
                            "Safety",
                            "Safety with Competitiveness Loss",
                            "Japan",
                            "United States",
                            "Age 37-52",
                            "Age 53-64",
                            "Age over 64",
                            "Male",
                            "Education Medium",
                            "Education High",
                            "Japan : Competitiveness with Safety Loss",
                            "Japan : Safety",
                            "Japan : Safety with Competitiveness Loss",
                            "United States : Competitiveness with Safety Loss",
                            "United States : Safety",
                            "United States : Safety with Competitiveness Loss",
                            "Male : Competitiveness with Safety Loss",
                            "Male : Safety",
                            "Male : Safety with Competitiveness Loss",
                            "Age 37-52 : Competitiveness with Safety Loss",
                            "Age 37-52 : Safety",
                            "Age 37-52 : Safety with Competitiveness Loss",
                            "Age 53-64 : Competitiveness with Safety Loss",
                            "Age 53-64 : Safety",
                            "Age 53-64 : Safety with Competitiveness Loss",
                            "Age over 64 : Competitiveness with Safety Loss",
                            "Age over 64 : Safety",
                            "Age over 64 : Safety with Competitiveness Loss",
                            "Negative Attitudes Algorithms",
                            "Negative Attitudes Algorithms : Competitiveness with Safety Loss",
                            "Negative Attitudes Algorithms : Safety",
                            "Negative Attitudes Algorithms : Safety with Competitiveness Loss",
                            "Technology Optimism",
                            "Technology Optimism : Competitiveness with Safety Loss",
                            "Technology Optimism : Safety",
                            "Technology Optimism : Safety with Competitiveness Loss",
                            "Economic Left-Right",
                            "Economic Left-Right : Competitiveness with Safety Loss",
                            "Economic Left-Right : Safety",
                            "Economic Left-Right : Safety with Competitiveness Loss"),
                          file="Table2.html")
tab2


##############################
### Build Results Figures  ###
##############################

#################################################################
### Figure 1 Baseline Framing Effects (Pooled and by Sample)  ###
#################################################################

me1$group <- "Pooled"
me2 <- rbind(me1,me2)

me2$group <- fct_relevel(me2$group,"Pooled", after=0)
me2$group <- fct_relevel(me2$group,"US", after=1)
me2$group <- fct_relevel(me2$group,"JP", after=2)

levels(me2$group) <- c("Pooled","United States","Japan","Germany")
levels(me2$x) <- c("Competitiveness","Competitiveness \nSafety Loss","Safety","Safety \nCompetitiveness Loss")

fig1 <- ggplot(me2,aes(x,predicted))+
  geom_col()+
  geom_errorbar(aes(ymin=conf.low,ymax=conf.high),width=.25)+
  theme_classic()+
  aes(fill=x)+
  labs(x="",y="Agreement")+
  facet_wrap(~group,ncol=4)+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        strip.text = element_text(size = 16,face="bold"),
        axis.text.y = element_text(size=14,face="plain"),
        text=element_text(size=14,face="italic"),
        axis.text.x = element_blank(),
        axis.ticks.x=element_blank(),
        legend.text=element_text(size=12,face="bold"))+
  scale_color_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_fill_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_y_continuous(breaks=seq(1,11,by=1))
fig1

ggsave(fig1,file="Figure1.PNG",dpi=500,width=12,height=6)


##########################################
### Figure 2 Framing Effects by Gender ###
##########################################

#Prepare for Plotting
me3$facet <- "\n"
levels(me3$x) <- c("Competitiveness","Competitiveness \nSafety Loss","Safety","Safety \nCompetitiveness Loss")

##plot Marginal Means

fig2 <- ggplot(me3,aes(x,predicted),colors="bw")+
  geom_col(aes(ymin=conf.low,ymax=conf.high,group=group,fill=group,
               shape=group),position=position_dodge2(width=.5))+
  geom_errorbar(aes(ymin=conf.low,ymax=conf.high,group=group),
                position=position_dodge2(width=.25),size=.5)+
  theme_classic()+
  facet_nested(.~facet,scales="free")+
  labs(x="",y="Agreement")+
  scale_color_manual(values=c("#FC9918","#370665"))+
  scale_fill_manual(values=c("#FC9918","#370665"))+
  scale_y_continuous(limits=c(0,11),breaks=seq(1,11,by=1))+
  theme(legend.position=c(0.025,1.02),
        legend.direction="horizontal",
        legend.justification=c(0,0),
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="plain"),
  )
fig2

ggsave(fig2,filename="Figure2.PNG",width=8,height=5.5)

#############################################
### Figure 3 Framing Effects by Age Group ###
#############################################

me4$facet <- "\n"
levels(me4$x) <- c("Competitiveness","Competitiveness \nSafety Loss","Safety","Safety \nCompetitiveness Loss")
levels(me4$group) <- c("18-37","38-52","53-64","65-97")

fig3 <- ggplot(me4,aes(x,predicted),colors="bw")+
  geom_col(aes(ymin=conf.low,ymax=conf.high,group=group,fill=group,
               shape=group),position=position_dodge2(width=.5))+
  geom_errorbar(aes(ymin=conf.low,ymax=conf.high,group=group),
                position=position_dodge2(width=.25),size=.5)+

  theme_classic()+
  facet_nested(.~facet,scales="free")+
  labs(x="",y="Agreement")+
  scale_color_manual(values=c("#370665","#35589A","#FC9918","#F14A16"))+
  scale_fill_manual(values=c("#370665","#35589A","#FC9918","#F14A16"))+
  scale_y_continuous(limits=c(0,11),breaks=seq(1,11,by=1))+
  theme(legend.position=c(0.025,1.02),
        legend.direction="horizontal",
        legend.justification=c(0,0),
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="plain"),
  )
fig3

ggsave(fig3,filename="Figure3.PNG",width=8,height=5.5)


##################################################################################
### Figure 4  Framing effects by Negative Attitudes towards Algorithms (Index) ###
##################################################################################

me5$frame <- ifelse(me5$group=="Economy" | me5$group=="Economy vs. Safety","Competitiveness","Safety")
me5$tradeoff <- ifelse(me5$group=="Economy" | me5$group=="Safety","without loss","with loss")
me5$tradeoff <- fct_rev(me5$tradeoff)

fig4 <- ggplot(me5,aes(x,predicted),colors="bw")+
  theme_classic()+
  facet_grid(~frame)+
  labs(x="Negative Attitudes towards Algorithms",y="Agreement")+
  geom_line(aes(linetype=tradeoff,color=group),size=1)+
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high,fill=group),alpha=.7)+
  scale_color_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_fill_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_x_continuous(limits=c(1,5))+
  scale_y_continuous(breaks=seq(1,11,by=1))+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=12,face="plain"),
        axis.text.y = element_text(size=12,face="plain"),
        text=element_text(size=14,face="italic"))+
  guides(color="none",fill="none")
fig4

ggsave(fig4,file="Figure4.PNG",dpi=888,width=8,height=5)


################################################################
### Figure 5  Framing effects by Technology Optimism (Index) ###
################################################################

me6$frame <- ifelse(me6$group=="Economy" | me6$group=="Economy vs. Safety","Competitiveness","Safety")
me6$tradeoff <- ifelse(me6$group=="Economy" | me6$group=="Safety","without loss","with loss")
me6$tradeoff <- fct_rev(me6$tradeoff)

fig5 <- ggplot(me6,aes(x,predicted),colors="bw")+
  geom_line(aes(linetype=tradeoff,color=group),size=1)+
  theme_classic()+
  facet_grid(~frame)+
  labs(x="Technology Optimism",y="Agreement")+
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high,fill=group),alpha=.7)+
  scale_color_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_fill_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_x_continuous(limits=c(1,5))+
  scale_y_continuous(breaks=seq(1,11,by=1))+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=12,face="plain"),
        axis.text.y = element_text(size=12,face="plain"),
        text=element_text(size=14,face="italic"))+
  guides(color="none",fill="none")
fig5

ggsave(fig5,file="Figure5.PNG",dpi=500,width=8,height=5)

#######################################################
### Figure 6  Framing effects by Political Ideology ###
#######################################################

me7$frame <- ifelse(me7$group=="Economy" | me7$group=="Economy vs. Safety","Competitiveness","Safety")
me7$tradeoff <- ifelse(me7$group=="Economy" | me7$group=="Safety","without loss","with loss")
me7$tradeoff <- fct_rev(me7$tradeoff)

fig6 <- ggplot(me7,aes(x,predicted),colors="bw")+
  geom_line(aes(linetype=tradeoff,color=group),size=1)+
  theme_classic()+
  facet_grid(~frame)+
  labs(x="Economic Left-Right",y="Agreement")+
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high,fill=group),alpha=.7)+
  scale_color_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_fill_manual(values=c("#370665","#35589A","#F14A16","#FC9918"))+
  scale_x_continuous(limits=c(1,10),breaks=seq(1,11,by=1))+
  scale_y_continuous(breaks=seq(1,11,by=1))+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=12,face="plain"),
        axis.text.y = element_text(size=12,face="plain"),
        text=element_text(size=14,face="italic"))+
  guides(color="none",fill="none")
fig6

ggsave(fig6,file="Figure6.PNG",dpi=500,width=8,height=5)

######################
##### Appendix #######
######################

#################################
###Descriptive statistics########
#################################

########################################################
###### Table A1 Variables and Descriptive Statistics ###
########################################################

### Add Variable Labels ###

label(df$agegrp) <- "Age"
label(df$gender) <- "Gender"
label(df$sample) <- "Sample"
label(df$admanx) <- "Algorithm Anxiety"
label(df$tech) <- "Technology Optimism (Index)"
label(df$ecoLR) <- "Economic Left-Right"
label(df$admatt) <- "Negative Attitudes Algorithms (Index)"
label(df$climateeco) <- "Prioritize Climate vs. Economy"
label(df$tech1) <- "tech1"
label(df$tech4) <- "tech4"
label(df$tech5) <- "tech5"
label(df$tech6) <- "tech6"
label(df$tech7) <- "tech7"
label(df$tech8) <- "tech8"
label(df$tech9) <- "tech9"
label(df$ADMpreferhuman) <- "ADMpref"
label(df$edu) <- "Education"

descr <- table1(~agegrp+gender+edu+admanx+tech+tech1+tech4+tech5+tech6+tech7+tech8+tech9+ecoLR+climateeco+admatt+ADMmakeeasy+ADMpreferhuman|sample,
                data=df)
descr


########################################################
###### Figure A1 Distribution of Attitudinal Measures ##
########################################################

d1 <- select(df,c(ecoLR,sample))
d1$var <- "Economic Left-Right"
names(d1)[names(d1) == 'ecoLR'] <- 'x'
d2 <- select(df,c(admanx,sample))
d2$var <- "Algorithm Anxiety"
names(d2)[names(d2) == 'admanx'] <- 'x'
d3 <- select(df,c(tech,sample))
d3$var <- "Technology Optimism"
names(d3)[names(d3) == 'tech'] <- 'x'
d5 <- rbind(d1,d2,d3)
d5$var <- as.factor(d5$var)
levels(d5$var)
d5$var <- fct_relevel(d5$var,"Technology Optimism",after=1)

figa9 <- ggplot(d5,aes(x=x,y=sample,fill=sample))+
  geom_density_ridges2(scale=2,bandwidth=0.4,alpha=0.85,quantile_lines=T,quantiles=2,size=.7)+
  theme_classic()+
  scale_fill_manual(values=c("#F14A16","#370665","#35589A"))+
  labs(y="",x="")+
  guides(fill="none")+
  facet_wrap(~var,scales="free_x",ncol=3)+
  theme(text=element_text(size=16))+
  scale_x_continuous(breaks=seq(1,10,by=1))
figa9

ggsave(figa9,file="FigureA9.PNG",dpi=888,width=12,height=5)


#######################################
#### Figure A3: Effect of Tradeoff by Frame #####
#######################################

df$frames <- as.factor(as.character(ifelse(startsWith(as.character(df$frame),"Economy"),"Competitiveness","Safety")))
df$tradeoff <- as.factor(as.character(ifelse(grepl("vs",df$frame),"loss","gain")))

m2a <- lm(framingDV ~ frames+tradeoff+sample+gender+agegrp+
            edu+frames*tradeoff*sample,data=df)
ame2a <- summary(margins::margins(m2a,at=list(frames=levels(df$frames),
                                              sample = levels(df$sample))))

ame2a <- ame2a[grepl("trade",ame2a$factor),]
ame2a$factor <- ifelse(ame2a$frame == "Competitiveness","Safety Loss","Competitiveness Loss")

figa3 <- ggplot(ame2a,aes(x=factor,y=AME))+
  geom_pointrange(aes(ymin=lower,ymax=upper,group=sample,shape=sample,
                      color=frames),
                  position=position_dodge2(width=.5))+
  facet_grid(.~frames,scales="free_x")+
  geom_hline(yintercept=0)+
  theme_classic()+
  theme(legend.position=c(.8,1.035),
        legend.direction="horizontal",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        legend.background = element_blank(),
        strip.text = element_text(size=14,face="plain",hjust=0),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="bold",hjust=0),
        axis.title.x = element_text(size=14,face="italic"))+
  scale_y_continuous(breaks=seq(-10,10,by=.5))+
  scale_color_manual(values=c("#35589A","#FC9918","#F14A16"))+
  labs(x="",y="Average Marginal Effect")+
  guides(color="none")

ggsave(figa3,filename="FigureA3.PNG",dpi=500,width=8,height=5.5)

#########################################################
### Figure A2-A7 Average Marginal Effect for Subgroups ##
#########################################################

#create bins for numeric covariates

df$admbin <- cut_number(df$admatt,3)
df$techbin <- cut_number(df$tech,3)

# estimate binned estimates
m5b <- lm(framingDV ~ frame+agegrp+gender+edu+admbin+
            frame*admbin,data=df)
m6b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+techbin+
            frame*techbin,data=df)

# estimate average marginal effect by framing condtion
ame2 <- summary(margins::margins(m2,at=list(frame=levels(df$frame))))
ame3 <- summary(margins::margins(m3,at=list(frame=levels(df$frame))))
ame4 <- summary(margins::margins(m4,at=list(frame=levels(df$frame))))
ame5 <- summary(margins::margins(m5b,at=list(frame=levels(df$frame))))
ame6 <- summary(margins::margins(m6b,at=list(frame=levels(df$frame))))
ame <- bind_rows(ame2[grepl("sample",ame2$factor),],ame3[grepl("gender",ame3$factor),],
                 ame4[grepl("agegrp",ame4$factor),],ame5[grepl("admbin",ame5$factor),],
                 ame6[grepl("techbin",ame6$factor),])

# prepare for plotting
ame$title <- "\n"
levels(ame$frame) <- c("Competitiveness","Competitiveness\nSafety Loss","Safety","Safety\nCompetitiveness Loss")
ame$factor <- as.factor(as.character(ame$factor))
ame$group <- ame$factor
levels(ame$group) <- c("Medium vs. Low","Strong vs. Low","38-52","53-64","65-97","Male vs. Female","Japan vs. Germany","United States vs. Germany","Medium vs. Low","Strong vs. Low")

#plot results

# Figure A2
figa2 <- ggplot(ame[grepl("sample",ame$factor),],aes(x=frame,y=AME,group=group))+
  geom_pointrange(aes(ymin=lower,ymax=upper,color=group,shape=group),
                  position=position_dodge2(width=.5),
                  size=1,fatten=2)+
  geom_hline(yintercept=0)+
  facet_grid(.~title,)+
  theme_classic()+
  theme(legend.position=c(.3,1.05),
        legend.direction="horizontal",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain",hjust=0),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="bold",hjust=0),
        axis.title.x = element_text(size=14,face="italic"))+
  scale_y_continuous(breaks=seq(-10,10,by=.5))+
  scale_color_manual(values=c("#F14A16","#35589A"))+
  scale_fill_manual(values=c("#F14A16","#35589A"))+
  scale_shape_manual(values=c(16,17,18,15))+
  labs(x="",y="Average Marginal Effect")

# Figure A4

figa4 <- ggplot(ame[grepl("gender",ame$factor),],aes(x=frame,y=AME,group=group))+
  geom_pointrange(aes(ymin=lower,ymax=upper,color=group,shape=group),
                  position=position_dodge2(width=.5),
                  size=1,fatten=2)+
  geom_hline(yintercept=0)+
  facet_grid(.~title,)+
  theme_classic()+
  theme(legend.position=c(.13,1.05),
        legend.direction="horizontal",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain",hjust=0),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="bold",hjust=0),
        axis.title.x = element_text(size=14,face="italic"))+
  scale_y_continuous(breaks=seq(-10,10,by=.5))+
  scale_color_manual(values=c("#370665","#F14A16"))+
  scale_fill_manual(values=c("#370665","#F14A16"))+
  scale_shape_manual(values=c(16,17,18,15))+
  labs(x="",y="Average Marginal Effect")

# Figure A5

figa5 <- ggplot(ame[grepl("agegrp",ame$factor),],aes(x=frame,y=AME,group=group))+
  geom_pointrange(aes(ymin=lower,ymax=upper,color=group,shape=group),
                  position=position_dodge2(width=.5),
                  size=1,fatten=2)+
  geom_hline(yintercept=0)+
  facet_grid(.~title,)+
  theme_classic()+
  theme(legend.position=c(.18,1.06),
        legend.direction="horizontal",
        legend.title=element_blank(),
        legend.text=element_text(size=11,face="plain"),
        strip.text = element_text(size=14,face="plain",hjust=0),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="bold",hjust=0),
        axis.title.x = element_text(size=14,face="italic"))+
  scale_y_continuous(breaks=seq(-10,10,by=.5))+
  scale_color_manual(values=c("#35589A","#FC9918","#F14A16"))+
  scale_fill_manual(values=c("#35589A","#FC9918","#F14A16"))+
  scale_shape_manual(values=c(16,18,17,15))+
  labs(x="",y="Average Marginal Effect")

# Figure A6

figa6 <- ggplot(ame[grepl("adm",ame$factor),],aes(x=frame,y=AME))+
  geom_pointrange(aes(ymin=lower,ymax=upper,color=group,shape=group),
                  position=position_dodge2(width=.5),
                  size=1,fatten=2)+
  geom_hline(yintercept=0)+
  facet_grid(.~title,)+
  theme_classic()+
  theme(legend.position=c(.22,1.06),
        legend.direction="horizontal",
        legend.title=element_blank(),
        legend.text=element_text(size=11,face="plain"),
        strip.text = element_text(size=14,face="plain",hjust=0),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="bold",hjust=0),
        axis.title.x = element_text(size=14,face="italic"))+
  scale_y_continuous(breaks=seq(-10,10,by=.5))+
  scale_color_manual(values=c("#370665","#FC9918"))+
  scale_fill_manual(values=c("#370665","#FC9918"))+
  scale_shape_manual(values=c(16,17,18,15))+
  labs(x="",y="Average Marginal Effect")

# Figure A7

figa7 <- ggplot(ame[grepl("tech",ame$factor),],aes(x=frame,y=AME))+
  geom_pointrange(aes(ymin=lower,ymax=upper,color=group,shape=group),
                  position=position_dodge2(width=.5),
                  size=1,fatten=2)+
  geom_hline(yintercept=0)+
  facet_grid(.~title,)+
  theme_classic()+
  theme(legend.position=c(.22,1.06),
        legend.direction="horizontal",
        legend.title=element_blank(),
        legend.text=element_text(size=11,face="plain"),
        strip.text = element_text(size=14,face="plain",hjust=0),
        axis.text.x = element_text(size=11,face="bold",hjust=.5,vjust=1),
        axis.text.y = element_text(size=12,face="bold",hjust=0),
        axis.title.x = element_text(size=14,face="italic"))+
  scale_y_continuous(breaks=seq(-10,10,by=.5))+
  scale_color_manual(values=c("#370665","#FC9918"))+
  scale_fill_manual(values=c("#370665","#FC9918"))+
  scale_shape_manual(values=c(16,17,18,15))+
  labs(x="",y="Average Marginal Effect")


ggsave(figa2,filename="FigureA2.PNG",dpi=500,width=8,height=5.5)
ggsave(figa4,filename="FigureA4.PNG",dpi=500,width=8,height=5.5)
ggsave(figa5,filename="FigureA5.PNG",dpi=500,width=8,height=5.5)
ggsave(figa6,filename="FigureA6.PNG",dpi=500,width=8,height=5.5)
ggsave(figa7,filename="FigureA7.PNG",dpi=500,width=8,height=5.5)

###################################################################
### Figure A8 Framing effects by Algorithm Anxiety (single Item) ##
###################################################################

m9 <- lm(framingDV ~ frame+sample+agegrp+gender+edu+admanx+frame*admanx,data=df)
me9 <- ggeffect(m9, c("admanx[all]", "frame"), ci.lvl = 0.95)
me9$frame <- ifelse(me9$group=="Economy" | me9$group=="Economy vs. Safety","Competitiveness","Safety")
me9$tradeoff <- ifelse(me9$group=="Economy" | me9$group=="Safety","without loss","with loss")
me9$tradeoff <- fct_rev(me9$tradeoff)

figa8 <- ggplot(me9,aes(x,predicted),colors="bw")+
  geom_line(aes(linetype=tradeoff,color=group))+
  theme_classic()+
  facet_grid(~frame)+
  labs(x="Algorithm Anxiety",y="Agreement")+
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high,fill=group),alpha=.7)+
  scale_color_manual(values=c("#F14A16","#FC9918","#35589A","#370665"))+
  scale_fill_manual(values=c("#F14A16","#FC9918","#35589A","#370665"))+
  scale_x_continuous(breaks=seq(1,10,by=1))+
  scale_y_continuous(breaks=seq(1,10,by=1))+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=12,face="plain"),
        axis.text.y = element_text(size=12,face="plain"),
        text=element_text(size=14,face="italic"))+
  guides(color="none",fill="none")
figa7

ggsave(figa8,file="FigureA8.PNG",dpi=500,width=8,height=5.5)


##################################################################
#### Figure A9 Framing effects by Political Ideology (Ecology) ###
##################################################################

m10 <- lm(framingDV ~ frame+agegrp+gender+edu+frame*climateeco,data=df)
me10 <- ggeffect(m10, c("climateeco[all]", "frame"), ci.lvl = 0.95,interactive=T)

me10$frame <- ifelse(me10$group=="Economy" | me10$group=="Economy vs. Safety","Competitiveness","Safety")
me10$tradeoff <- ifelse(me10$group=="Economy" | me10$group=="Safety","without loss","with loss")
me10$tradeoff <- fct_rev(me10$tradeoff)

figa9 <- ggplot(me10,aes(x,predicted),colors="bw")+
  geom_line(aes(linetype=tradeoff,color=group))+
  theme_classic()+
  facet_grid(~frame)+
  labs(x="Prioritize Climate vs. Economy",y="Agreement")+
  geom_ribbon(aes(ymin=conf.low,ymax=conf.high,fill=group),alpha=.7)+
  scale_color_manual(values=c("#F14A16","#FC9918","#35589A","#370665"))+
  scale_fill_manual(values=c("#F14A16","#FC9918","#35589A","#370665"))+
  scale_x_continuous(breaks=seq(1,10,by=1))+
  scale_y_continuous(breaks=seq(1,10,by=1))+
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text=element_text(size=12,face="plain"),
        strip.text = element_text(size=14,face="plain"),
        axis.text.x = element_text(size=12,face="plain"),
        axis.text.y = element_text(size=12,face="plain"),
        text=element_text(size=14,face="italic"))+
  guides(color="none",fill="none")
figa9

ggsave(figa9,file="FigureA9.PNG",dpi=500,width=8,height=5.5)


###################
### Robustness ####
###################

## conduct analyses with all obs (incl response-time outliers)

dfb <- readRDS("Hemesath_Tepe_Framing_TiS_Replication.RDS.RDS")

dfb$sample <- as.factor(dfb$sample)
dfb$frame <- as.factor(dfb$frame)
dfb$gender <- ifelse(dfb$gndr=="Non-Binary",NA,as.character(dfb$gndr))
dfb$gender <- as.factor(dfb$gender)
dfb$agegrp <- cut_number(dfb$age,4)
dfb$tech5 <- recode(dfb$tech5,5,4,3,2,1)
dfb$ADMmakeeasy <- recode(dfb$ADMmakeeasy,5,4,3,2,1)
dfb$admanx <- dfb$ADMuneasy
dfb$admatt <- (dfb$ADMmakeeasy+dfb$admanx+dfb$ADMpreferhuman)/3
dfb$tech <- ((dfb$tech1+dfb$tech4+dfb$tech5+dfb$tech6+dfb$tech7+dfb$tech8+dfb$tech9)/7)
dfb$edu <- dfb$education
levels(dfb$edu) <- c("Low","Low","Low","Medium","High","High","Low","Low","Medium","Medium","High","High","High","High","Low","Low","Medium","High","High","High","High")

m1b <- lm(framingDV ~ frame+sample+agegrp+gender+edu,data=dfb)
m2b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+
           frame*sample,data=dfb)
m3b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+
           frame*gender,data=dfb)
m4b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+
           frame*agegrp,data=dfb)
m5b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+admatt+
           frame*admatt,data=dfb)
m6b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+tech+
           frame*tech,data=dfb)
m7b <- lm(framingDV ~ frame+sample+agegrp+gender+edu+ecoLR+
           frame*ecoLR,data=dfb)
m8b <- lm(framingDV ~ frame+sample+frame*sample+agegrp+gender+edu+ecoLR+tech+admatt+
           frame*gender+frame*agegrp+frame*ecoLR+frame*tech+frame*admatt,data=dfb)

tab_model(m1b,m2b,m3b,m4b,m5b,m6b,m7b,m8b,
          vcov.type = "HC3",
          show.se = TRUE,
          show.ci=F,
          p.style = "stars",
          collapse.se = T,
          dv.labels = c("(1)", "(2)", "(3)", "(4)","(5)","(6)","(7)","(8)"),
          show.reflvl=T,
          auto.label=F,
          show.fstat=T,
          show.aic=T,
          show.loglik = T,
          file="Table2_Full.html",
          pred.labels=c(
            "(Intercept)",
            "Competitiveness with Safety Loss",
            "Safety",
            "Safety with Competitiveness Loss",
            "Japan",
            "United States",
            "Age 37-51",
            "Age 52-63",
            "Age over 64",
            "Gender Male",
            "Education Medium",
            "Education High",
            "Japan : Competitiveness with Safety Loss",
            "Japan : Safety",
            "Japan : Safety with Competitiveness Loss",
            "United States : Competitiveness with Safety Loss",
            "United States : Safety",
            "United States : Safety with Competitiveness Loss",
            "Male : Competitiveness with Safety Loss",
            "Male : Safety",
            "Male : Safety with Competitiveness Loss",
            "Age 37-52 : Competitiveness with Safety Loss",
            "Age 37-52 : Safety",
            "Age 37-52 : Safety with Competitiveness Loss",
            "Age 53-64 : Competitiveness with Safety Loss",
            "Age 53-64 : Safety",
            "Age 53-64 : Safety with Competitiveness Loss",
            "Age over 64 : Competitiveness with Safety Loss",
            "Age over 64 : Safety",
            "Age over 64 : Safety with Competitiveness Loss",
            "Negative Attitudes Algorithms",
            "Negative Attitudes Algorithms : Competitiveness with Safety Loss",
            "Negative Attitudes Algorithms : Safety",
            "Negative Attitudes Algorithms : Safety with Competitiveness Loss",
            "Technology Optimism",
            "Technology Optimism : Competitiveness with Safety Loss",
            "Technology Optimism : Safety",
            "Technology Optimism : Safety with Competitiveness Loss",
            "Economic Left-Right",
            "Economic Left-Right : Competitiveness with Safety Loss",
            "Economic Left-Right : Safety",
            "Economic Left-Right : Safety with Competitiveness Loss"))

#################################
##### AME Framing Effect ########
#################################


